import java.awt.*;
import java.util.LinkedList;

public class Maps {
    static int map[][] = {
            {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
            {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
            {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
            {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
            {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
            {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
            {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
            {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
            {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
            {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
            {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
            {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
            {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
            {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
            {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
            {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
            {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
            {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
            {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
            {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
            {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
            {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
            {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}
    };


    public int mapsWidth = map[0].length * Global.CELL_SIZE;
    public int mapsHight = map.length * Global.CELL_SIZE;
    public int offsetX;
    public int offsetY;
    public int screenWidth = Global.GAMEWIDTH;
    public int screenHeight = Global.GAMEHEIGHT;

    public static int[][] getMap() {
        return map;
    }

    TurnBaseStats currentTurnBaseStats = TurnBaseStats.nothing;

    public TurnBaseStats getCurrentTurnBaseStats() {
        return currentTurnBaseStats;
    }

    public void setCurrentTurnBaseStats(TurnBaseStats currentTurnBaseStats) {
        this.currentTurnBaseStats = currentTurnBaseStats;
    }

    public void render(Graphics g, DoraemonObjectManager doraemonObjectManager, MedusaObjectManager medusaObjectManager, GameStats gameStats) {
        cameraFollowByPlayerOrEnemyies(doraemonObjectManager, medusaObjectManager, gameStats);

        initMap(g);
        AbstractBiology tempAbstractBiology = null;
        if (gameStats.CURRENTSTATS == gameStats.PLAYERTURN) {
            tempAbstractBiology = doraemonObjectManager.abstractBiologyLinked.get(doraemonObjectManager.getcurrentplayer);
        } else if (gameStats.CURRENTSTATS == gameStats.ENEMYTURN) {
            tempAbstractBiology = medusaObjectManager.enemyLinked.get(medusaObjectManager.getcurrentenemy);
        }
        if (getCurrentTurnBaseStats() == TurnBaseStats.playerturn) {
            showTipsPlayerOrEnemyTurn(g, "玩家的回合");
        } else if (getCurrentTurnBaseStats() == TurnBaseStats.enemyturn) {
            showTipsPlayerOrEnemyTurn(g, "敌人的回合");
        } else if (getCurrentTurnBaseStats() == TurnBaseStats.enemyAttck) {
            showAttackTips(g, "敌人的攻击:", doraemonObjectManager.abstractBiologyLinked, doraemonObjectManager.getcurrentplayer);
        } else if (getCurrentTurnBaseStats() == TurnBaseStats.playerAttck) {
            showAttackTips(g, "玩家的攻击:", medusaObjectManager.enemyLinked, medusaObjectManager.getcurrentenemy);
        }
    }

    private void showAttackTips(Graphics g, String s, LinkedList<AbstractBiology> abstractBiologyLinked, int getcurrentplayer) {
        Image msg = initDrawFontAndImage(g);
        g.drawImage(msg, 0, Global.GAMEHEIGHT - msg.getHeight(null) - Global.CELL_SIZE / 2, Global.GAMEWIDTH, msg.getHeight(null), null);
        g.drawString(s + abstractBiologyLinked.get(getcurrentplayer).getName(), Global.CELL_SIZE * 2, Global.GAMEHEIGHT - msg.getHeight(null) / 2 - 20);
    }

    private Image initDrawFontAndImage(Graphics g) {
        g.setColor(Color.ORANGE);
        Image msg = Toolkit.getDefaultToolkit().getImage("image/地图/msg.png");
        Font font1 = new Font("宋体", Font.BOLD, msg.getHeight(null) / 2);
        g.setFont(font1);
        return msg;
    }

    private void showTipsPlayerOrEnemyTurn(Graphics g, String 玩家的回合) {
        Image msg = initDrawFontAndImage(g);
        g.drawImage(msg, 0, Global.GAMEHEIGHT - msg.getHeight(null) - Global.CELL_SIZE / 2, Global.GAMEWIDTH, msg.getHeight(null), null);
        g.drawString(玩家的回合, Global.CELL_SIZE * 2, Global.GAMEHEIGHT - msg.getHeight(null) / 2 - 20);
    }

    private void initMap(Graphics g) {
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++)
                if (map[i][j] == 1) {
                    drawMapObject(g, i, j, Color.GREEN, "image/地图/冰水深明星.png");
                } else if (map[i][j] == 2) {
                    drawMapObject(g, i, j, Color.red, "image/地图/金属圆角.png");
                }
        }
    }

    private void drawMapObject(Graphics g, int i, int j, Color green, String s) {
        g.setColor(green);
        g.fill3DRect(offsetX + j * Global.CELL_SIZE,
                offsetY + i * Global.CELL_SIZE,
                Global.CELL_SIZE, Global.CELL_SIZE, true);
        Image image = Toolkit.getDefaultToolkit().getImage(s);
        g.drawImage(image, offsetX + j * Global.CELL_SIZE,
                offsetY + i * Global.CELL_SIZE,
                Global.CELL_SIZE, Global.CELL_SIZE, null);
    }

    private void cameraFollowByPlayerOrEnemyies(DoraemonObjectManager doraemonObjectManager, MedusaObjectManager medusaObjectManager, GameStats gameStats) {
        if (gameStats.CURRENTSTATS == gameStats.PLAYERTURN) {
            followWithObject(doraemonObjectManager.abstractBiologyLinked, doraemonObjectManager.getcurrentplayer);
        } else if (gameStats.CURRENTSTATS == gameStats.ENEMYTURN) {
            followWithObject(medusaObjectManager.enemyLinked, medusaObjectManager.getcurrentenemy);
        }
    }

    private void followWithObject(LinkedList<AbstractBiology> abstractBiologyLinked, int getcurrentplayer) {
        if (abstractBiologyLinked.get(getcurrentplayer).getX() * Global.CELL_SIZE <= screenHeight / 2) {
            offsetY = 0;
        } else if (abstractBiologyLinked.get(getcurrentplayer).getX() * Global.CELL_SIZE >= mapsHight - screenHeight / 2) {
            offsetY = screenHeight - mapsHight;
        } else {
            offsetY = screenHeight / 2 - abstractBiologyLinked.get(getcurrentplayer).getX() * Global.CELL_SIZE;
        }
        if (abstractBiologyLinked.get(getcurrentplayer).getY() * Global.CELL_SIZE <= screenWidth / 2) {
            offsetX = 0;
        } else if (abstractBiologyLinked.get(getcurrentplayer).getY() * Global.CELL_SIZE >= mapsWidth - screenWidth / 2) {
            offsetX = screenWidth - mapsWidth;
        } else {
            offsetX = screenWidth / 2 - abstractBiologyLinked.get(getcurrentplayer).getY() * Global.CELL_SIZE;
        }
    }
}
